﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "UserWidget/WidgetComponent/InMissionWidget.h"
#include "UserWidget/WidgetComponent/SingleObjectiveWidget.h"
#include "Components/TextBlock.h"
#include "Components/VerticalBox.h"
#include "Libraries/CommonLibrary.h"
#include "Libraries/GameKitFL.h"

void UInMissionWidget::NativeConstruct() {
	Super::NativeConstruct();
	CanSwitchObjectives = true;

}

void UInMissionWidget::NativeOnInitialized() {
	Super::NativeOnInitialized();
	CanSwitchObjectives = true;
}

void UInMissionWidget::ConstructWidget(FMissionMaster LCurrentMission)
{

}

void UInMissionWidget::ResetInMissionWidget()
{
	CurrentSequence = 0;
	CurrentObjectives.Empty();
	ObjectiveWidgets.Empty();
	ObjectivesVisibility = EToggleVisibility::Hidden;
}

void UInMissionWidget::UpdateSequence(FMissionMaster LCurrentMission, int32 LCurrentSequence){
	CurrentMission = LCurrentMission;
	CurrentSequence = LCurrentSequence;
	RefreshTexts();
}

void UInMissionWidget::RefreshTexts(){
	if (CurrentSequence != 0) {
		TempSequenceDesc->SetText(CurrentMission.Sequences[CurrentSequence].SequenceDescription);
		PlayAnimation(UCommonLibrary::GetAnimInstance(GetClass(), FText::FromString("SequenceUpdated")));
		FTimerHandle UniqueHandle;
		FTimerDelegate RespawnDelegate = FTimerDelegate::CreateLambda([this]() {
			SequenceTitle->SetText(CurrentMission.Sequences[CurrentSequence].SequenceTitle);
		});
		GetWorld()->GetTimerManager().SetTimer(UniqueHandle, RespawnDelegate, 0.25, false);
	}
	else {
		TempSequenceDesc->SetText(CurrentMission.Sequences[CurrentSequence].SequenceDescription);
		SequenceDescription->SetText(CurrentMission.Sequences[CurrentSequence].SequenceDescription);
		SequenceTitle->SetText(CurrentMission.Sequences[CurrentSequence].SequenceTitle);
	}
}

void UInMissionWidget::ConstructInMissionWidget(FMissionMaster LCurrentMission){
	CurrentMission = LCurrentMission;
	ResetInMissionWidget();
	RefreshTexts();
	UpdateObjectives(CurrentMission, CurrentSequence);
}

void UInMissionWidget::ToggleVisibility(EToggleVisibility InVisibility, bool InRemove){
	if (!UGameKitFL::InGameMenuOpened()) {
		PlayAnimation(UCommonLibrary::GetAnimInstance(GetClass(), FText::FromString("Slide")),0.f,1, InVisibility== EToggleVisibility::Visible?EUMGSequencePlayMode::Reverse:EUMGSequencePlayMode::Forward);
	}
	if (InRemove) {
		ObjectiveWidgets.Empty();
		ObjectivesPanel->ClearChildren();
		AddedObjective = 0;
		ObjectivesVisibility = EToggleVisibility::Hidden;
		RemoveFromParent();
		CurrentSequence = 0;
	}
}

void UInMissionWidget::AddObjective(FObjectives ObjectiveInfo){
	USingleObjectiveWidget* Temp = Cast<USingleObjectiveWidget>(CreateWidget(this, SingleObjectiveWidget));
	ObjectiveWidgets.Add(Temp);
	if (ObjectiveInfo.ObjectiveLayout.DisplayInObjectives) {
		ObjectivesPanel->AddChildToVerticalBox(Temp);
	}
}

void UInMissionWidget::SetObjectives(FMissionMaster LCurrentMission, int32 LCurrentSequence){
	if (LCurrentSequence == 0) {
		PlayAnimation(UCommonLibrary::GetAnimInstance(GetClass(), FText::FromString("QuestUpdated")));
	}
	CurrentObjectives = LCurrentMission.Sequences[LCurrentSequence].Objectives;
	for (int32 i = 0; i < CurrentObjectives.Num(); ++i) {
		AddObjective(CurrentObjectives[i]);
	}
}